﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Data;
using BarberShop.Model;
namespace BarberShop.BLL
{
    public class BrandInfoBLL
    {
        public static IList<BrandInfoModel> GetAllBrandList()
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = (from a in data.BrandInfo select new BrandInfoModel { BrandName = a.BrandName, CreateDate = a.CreateDate, ID = a.ID, ModifyDate = a.ModifyDate }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 获取品牌
        /// </summary>
        /// <returns></returns>
        public static BrandInfo GetBrandById(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.BrandInfo where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        /// <summary>
        /// 添加品牌
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool SaveBrandInfo(BrandInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    if (info.ID == 0)
                        data.BrandInfo.InsertOnSubmit(info);
                    else
                    {
                        ModifyBrandInfo(info);
                    }
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool ModifyBrandInfo(BrandInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.BrandInfo where a.ID == info.ID select a).FirstOrDefault();
                    r.BrandName = info.BrandName;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }

    }
}
